package com.oj.util;

import lombok.extern.slf4j.Slf4j;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.File;

@Slf4j
public class PdmlToHtmlConverter {
    public static String hexPacketDump(String pdmlPath, String htmlPath) {
        try {
            // 加载XSLT样式表
            StreamSource stylesheet = new StreamSource(new File("pdml2html.xsl"));

            // 创建一个TransformerFactory
            TransformerFactory transformerFactory = TransformerFactory.newInstance();

            // 通过TransformerFactory获取Transformer
            Transformer transformer = transformerFactory.newTransformer(stylesheet);

            // 设置输入和输出源
            StreamSource pdmlSource = new StreamSource(new File(pdmlPath));
            StreamResult htmlResult = new StreamResult(new File(htmlPath));
            // 执行转换
            transformer.transform(pdmlSource, htmlResult);
            return StreamHandler.readEx(htmlPath);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
    public static void main(String[] args) {
        try {
            log.info(hexPacketDump("pkt.pdml", "output.html"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
